package cn.liye.pingfen.service.impl;

import cn.liye.pingfen.mapper.ComReviewsMapper;
import cn.liye.pingfen.mapper.CompanyMapper;
import cn.liye.pingfen.pojo.dto.ComReviewsDTO;
import cn.liye.pingfen.pojo.entity.ComReviews;
import cn.liye.pingfen.pojo.vo.ComReviewsVO;
import cn.liye.pingfen.service.IComReviewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ComReviewService implements IComReviewsService {
    @Autowired
    ComReviewsMapper comReviewsMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Override
    public void insertComreviews(ComReviewsDTO comReviewsDTO) {
        String str = comReviewsDTO.getDetailedDescription();
        String words[] = {"畜生", "牛逼", "垃圾", "共产党", "傻逼", "他妈的", "操你妈"};
        for (int i = 0; i < words.length; i++) {
            if (str.matches(".*" + words[i] + ".*")) {
//          计算需要的*的个数
                String s = "";
                for (int j = 0; j < words[i].length(); j++) {
                    s += "*";
                }
                str = str.replaceAll(words[i], s);
                System.out.println(str);
            }
        }
        ComReviews comReviews = new ComReviews();
        BeanUtils.copyProperties(comReviewsDTO, comReviews);
        comReviews.setCreateTime(new Date());
        comReviews.setDetailedDescription(str);
        comReviewsMapper.insert(comReviews);
    }


    @Override
    public PageInfo<ComReviewsVO> listById(Long companyId, int pageNum, int pageSize) {
        // 开启分页，pageNum 是页码（从1开始），pageSize 是每页显示的记录数
        PageHelper.startPage(pageNum, pageSize);
        List<ComReviewsVO> comReviewsVO = comReviewsMapper.selectById(companyId);
        // 使用PageInfo包装查询结果
        PageInfo<ComReviewsVO> pageInfo = new PageInfo<>(comReviewsVO);
        return pageInfo;

    }

    @Override
    public Double selectsum(Long companyId) {
        Double sum = comReviewsMapper.selectsum(companyId);
        return sum;
    }

    @Override
    public int selectcount(Long companyId) {
        int count = comReviewsMapper.selectcount(companyId);

        return count;
    }

}